package com.itsu.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author 苏犇
 * @create time 2019/12/4 21:06
 */
@Configuration
@ComponentScan(basePackages = {"com.itsu.service","com.itsu.dao","com.itsu.compoment"})
@EnableTransactionManagement
@MapperScan(basePackages = "com.itsu.dao")
//@EnableScheduling
@ImportResource(locations = "classpath:spring-rabbitmq.xml")
public class AppConfig {

    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/local_db?useSSL=false&useUnicode=true&amp;characterEncoding=UTF8");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return druidDataSource;
    }

    @Bean
    public MybatisSqlSessionFactoryBean sessionFactoryBean(){
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
//        Resource mapperResource = new ClassPathResource("classpath:mappers/MessageMapper.xml");
//        sqlSessionFactoryBean.setMapperLocations(mapperResource);
        return sqlSessionFactoryBean;
    }


    @Bean
    public DataSourceTransactionManager transactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }
}
